using System;
using System.ComponentModel;
using System.IO;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class BackupFilesStep : ActionStep
    {
        private string m_srcDir = StepConstants.PATH;
        private string m_destDir = StepConstants.PATH;
        private string m_prefix = "Target";
        private string m_fileFilter = "*.*";

        [DisplayName( "Destination Dir" )]
        [Description( "The destination directory path in which a backup folder is created." )]
        public string DestDir
        {
            get
            {
                return m_destDir;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Destination Dir cannot be empty." );
                }
                else
                {
                    m_destDir = value.Trim();
                }
            }
        }

        [DisplayName( "File Filter" )]
        [Description( "The filter for files to backup." )]
        public string FileFilter
        {
            get
            {
                return m_fileFilter;
            }
            set
            {
                m_fileFilter = value.Trim();
            }
        }

        [Description( "The prefix for the backup folder name that is created in the destination dir." )]
        public string Prefix
        {
            get
            {
                return m_prefix;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Prefix cannot be empty." );
                }
                else
                {
                    m_prefix = value.Trim();
                }
            }
        }

        [DisplayName( "Source Dir" )]
        [Description( "The source directory path from where files are copied." )]
        public string SrcDir
        {
            get
            {
                return m_srcDir;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Source Dir cannot be empty." );
                }
                else
                {
                    m_srcDir = value.Trim();
                }
            }
        }

        public override string Name
        {
            get { return "Backup Files"; }
        }

        protected override void Run()
        {
            DirectoryHelper helper = new DirectoryHelper( Progress );
            helper.Copy( m_srcDir, getBackupDir(), m_fileFilter, true );
        }

        protected override void Simulate()
        {
            // Check for source dir
            DirectoryInfo dir = new DirectoryInfo( m_srcDir );
            if( !dir.Exists )
            {
                throw new Exception( string.Format( "Source dir {0} does not exist.", m_srcDir ) );
            }

            // Check for dest dir
            dir = new DirectoryInfo( m_destDir );
            if( !dir.Exists )
            {
                throw new Exception( string.Format( "Dest dir {0} does not exist.", m_destDir ) );
            }

            Progress.AddFormat( "Simulated backup from {0} to {1}.", m_srcDir, getBackupDir() );
        }

        private string getBackupDir()
        {
            return Path.Combine( m_destDir,
                string.Format( "{0}.{1}.{2}", m_prefix, DateTime.Now.ToString( "MM-dd-yyyy.hh.mm" ), Common.UserName ) );
        }
    }
}
